#!/bin/bash

function user_continue() {
  read -p "Do you want to continue? [y/n]" -n 1 -r
  echo
  if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    exit 1
  fi
}

if [ "$#" -ne 1 ]; then
  echo "USAGE: `basename $0` <repo-path>"
  exit
fi

REPO_PATH="${1}"
SHA=`git rev-parse --verify HEAD`
echo "================================================================="
echo "BEGIN PUBLISH SNAPSHOT for revision ${SHA} at ${REPO_PATH}"
echo "================================================================="
user_continue

# Build snapshot jars
./mvn_cmd --with-jdk8 clean
mvn package -Pjdk8 -Dmaven.javadoc.skip=true -B -V

# Pull latest from the maven repo
pushd ${REPO_PATH}
git pull
popd

# Deploy the artifact to the maven repo
mvn deploy -Pjdk8 -DskipTests -DaltDeploymentRepository=snapshot::default::file://${REPO_PATH}

# Push changes to the maven repo
pushd ${REPO_PATH}
git add .
git commit -a -m "Publish snapshot: ${SHA}"
git push origin master
popd

echo "Successfully published SNAPSHOT artifacts for ${SHA}"
